package dataAccess;

import java.sql.*;
import java.util.*;

import dataAccess.Connector;
import dataAccess.DALException;
import dataAccess.HouseDTO;

public class MySQLHouseDAO implements HouseDAO{
	public HouseDTO getHouse(int HouseID) throws DALException {
		ResultSet rs = Connector
				.doQuery("SELECT * FROM house WHERE House_ID = " + HouseID);

		try {
			if (!rs.first())
				throw new DALException("Der eksisterer ingen huse p� plads: "
						+ HouseID);
			return new HouseDTO(rs.getInt("House_ID"), rs.getInt("Field_ID"),
					rs.getInt("Houses"), rs.getBoolean("Hotel"));
		} catch (SQLException e) {
			throw new DALException(e);
		}
	}

	public List<HouseDTO> getHouseList() throws DALException {
		List<HouseDTO> data = new ArrayList<HouseDTO>();
		ResultSet rs = Connector.doQuery("SELECT * FROM House");
		try {
			while (rs.next()) {
				data.add(new HouseDTO(rs.getInt("House_ID"), rs
						.getInt("Field_ID"), rs.getInt("Houses"), rs
						.getBoolean("Hotel")));
			}
		} catch (SQLException e) {
			throw new DALException(e);
		}
		return data;
	}

	public void createHouse(HouseDTO house) throws DALException {
		Connector.doUpdate("INSERT INTO House VALUES(" + house.getHouseID()
				+ ", " + house.getFieldID() + ", " + house.getHouses() + ", "
				+ house.getHotel() + ");");
	}

	public void updateHouse(HouseDTO house) throws DALException {
		Connector.doUpdate("UPDATE House SET Houses  = " + house.getHouses()
				+ ", Hotel = " + house.getHotel() + " WHERE House_ID = "
				+ house.getHouseID() + ";");
	}

}
